package dao;

import java.io.Serializable;
import java.util.List;

import modelo.Domicilio;
import modelo.Provincia;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import util.HibernateUtil;

public class DomicilioDao implements Serializable {

	private static final long serialVersionUID = 447440060036221550L;

	private Session session;
	SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	
	public DomicilioDao() {
		super();
	}
	
//	public List<Domicilio> obtenerProvincias() {
//		session = sessionFactory.openSession();
//
//		Criteria cr = session.createCriteria(Domicilio.class);
//		List<Domicilio> prov = cr.list();
//		
//		session.close();
//
//		return prov;
//	}
	
	public void guardarDomicilio(Domicilio dom) {
		session = sessionFactory.openSession();
		session.beginTransaction();
		session.saveOrUpdate(dom);
		session.getTransaction().commit();
		session.close();
	}

	public int obtenerIdDomicilio(Domicilio dom){
		session = sessionFactory.openSession();
		
		Criteria cr = session.createCriteria(Domicilio.class);
		cr.add(Restrictions.eq("barrio", dom.getBarrio()));
		cr.add(Restrictions.eq("calle", dom.getCalle()));
		cr.add(Restrictions.eq("numero", dom.getNumero()));
		cr.add(Restrictions.eq("piso", dom.getPiso()));
		
		List<Domicilio> results = cr.list();
		return results.get(0).getIdDomicilio();
		
	}
	
}
